package com.kabigon.tiny.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.kabigon.tiny.componet.PersonFactoryBean;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author kabigon
 * @version 2022/9/19/02:02
 */
@Configuration
@PropertySource("classpath:jdbc.properties")
@MapperScan("com.kabigon.tiny.mapper")
@ComponentScan("com.kabigon.tiny")
public class MainConfig {

    @Value("${jdbc.driver}")
    private String jdbcClass;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public PersonFactoryBean personFactoryBean() {
        return new PersonFactoryBean();
    }

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(jdbcClass);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.getProxyFilters().add(wallFilter());
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**/*Mapper.xml"));
        return sqlSessionFactoryBean;
    }

    @Bean
    public WallFilter wallFilter() {
        WallFilter wallFilter = new WallFilter();

        // 配置WallFilter的属性
        wallFilter.setConfig(wallConfig());
        wallFilter.setLogViolation(true); // 设置记录违规日志
        wallFilter.setThrowException(false); // 可以设置为false，以防止抛出异常中断应用

        return wallFilter;
    }

    @Bean
    public WallConfig wallConfig() {
        WallConfig config = new WallConfig();

        // 启用或禁用各种防火墙保护措施
        config.setMultiStatementAllow(false); // 不建议允许一次执行多条SQL语句
        config.setConditionLikeTrueAllow(false); // 防止LIKE语句总是返回true。
        config.setConditionAndAlwayTrueAllow(false); // 防止条件语句中的AND子句总是返回true
        config.setSelectWhereAlwayTrueCheck(true); // 允许非基本语句
        config.setSelectHavingAlwayTrueCheck(true); // 允许非基本语句
        config.setDeleteWhereAlwayTrueCheck(true); // 检查DELETE语句的WHERE子句是否总是返回true
        config.setUpdateWhereAlayTrueCheck(true); // 检查UPDATE语句的WHERE子句是否总是返回true
        config.setNoneBaseStatementAllow(true); // 允许非基本语句
        config.setStrictSyntaxCheck(true); // 开启严格的语法检查可以帮助识别异常或非标准的SQL查询
        config.setCommentAllow(false); // SQL注释也可以被用于SQL注入攻击，建议不允许注释。
        // 更多配置...

        return config;
    }

}
